﻿using FlowCanvas;
using FlowCanvas.Nodes;
using ParadoxNotion.Design;
using RootMotion.FinalIK;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class StartTakeMoney : LatentActionNodeBase
    {
        //装钱的动画集合
        public static Dictionary<int,int> takeMoneyAnimSet;
        //装钱最大的播放段数
        public static int MaxStage;
        public static PlayerAnimatorEvent playerAnimEvent;
        [HideInInspector]
        public FlowOutput onTakeMoneyFinish;
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>(parameters[0].Name);
            var p2 = node.AddValueInput<string>(parameters[1].Name);
            var p3 = node.AddValueInput<GameObject>(parameters[2].Name);
            var p4 = node.AddValueInput<int>(parameters[3].Name);
            var p5 = node.AddValueInput<string>(parameters[4].Name);

            onTakeMoneyFinish = node.AddFlowOutput("onTakeMoneyFinish");

            node.AddFlowInput("In", (f) => {
                Begin(Invoke(p1.value, p2.value, p3.value,p4.value,p5.value), f);
            });
        }

        public IEnumerator Invoke(int moneyId, string animName, GameObject moneyObj,int maxStage,string animationPrefix)
        {
            if(takeMoneyAnimSet == null)
            {
                takeMoneyAnimSet = new Dictionary<int, int>();
                MaxStage = maxStage;
                for (int i=1;i<= maxStage; ++i)
                {
                    string temp = animationPrefix + i.ToString();
                    takeMoneyAnimSet.Add(Animator.StringToHash(temp),i);
                }
            }

            ResidentHandler.Inst.GetFunction("CityLuaFunction.StartTakeMoney").Action<int,string,Action,GameObject>(moneyId, animName, OnTakeMoneyFinish, moneyObj);
            var npcObj = BlueprintUtils.GetNpc(BlueprintUtils.PLAYER_NPC_ID);
            if (npcObj)
            {
                var playerAnimatorEvent = npcObj.transform.GetComponent<PlayerAnimatorEvent>();
                playerAnimEvent = playerAnimatorEvent;
                if (playerAnimatorEvent!=null)
                {
                    playerAnimatorEvent.MoneyObj = moneyObj;
                    playerAnimatorEvent.MoneyId = moneyId;
                }
/*                var FullBodyBipedIK = npcObj.transform.GetComponent<FullBodyBipedIK>();
                if (FullBodyBipedIK!=null)
                {
                    FullBodyBipedIK.solver.IKPositionWeight = 1;
                }*/
            }

            yield return new UnityEngine.WaitUntil(() => BlueprintUtils.UIIsShowing("PrologueUI"));
        }

        private void OnTakeMoneyFinish()
        {
            onTakeMoneyFinish?.Call(new Flow());
            takeMoneyAnimSet = null;
            playerAnimEvent = null;
            var npcObj = BlueprintUtils.GetNpc(BlueprintUtils.PLAYER_NPC_ID);
            if (npcObj)
            {
                var playerAnimatorEvent = npcObj.transform.GetComponent<PlayerAnimatorEvent>();
                /*if (playerAnimatorEvent != null)
                {
                    playerAnimatorEvent.ResetMoneyObj();
                }*/

/*                var FullBodyBipedIK = npcObj.transform.GetComponent<FullBodyBipedIK>();
                if (FullBodyBipedIK != null)
                {
                    FullBodyBipedIK.solver.IKPositionWeight = 0;
                }*/
                if (playerAnimatorEvent.MoneyObj)
                {
                    Animator moneyAnim = playerAnimatorEvent.MoneyObj.GetComponent<Animator>();
                    moneyAnim.SetTrigger("end_take_money_state");
                }
            }
        }
    }
}
